package com.neusoft.demo11;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class DBUtil {
    Connection conn;
    PreparedStatement pstmt;
    /**
     * 连接数据库
     */
    public Connection getConnection() throws ClassNotFoundException, SQLException {
        //1. 加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2. 获取连接  mysql 3306  oracle 1521
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/basicdemo17","root","123456");
        return conn;
    }
    /**
     * 查询
     */
    public ResultSet query(String sql,Object...values) throws SQLException {  //select * from User
        pstmt = conn.prepareStatement(sql);
        //将sql语句中的？替换为具体的值
        for (int i = 0;i<values.length;i++) {
            pstmt.setObject(i + 1,values[i]);
        }
        ResultSet rs = pstmt.executeQuery();
        return rs;
    }

    /**
     * dml
     */
    public int update(String sql,Object...values) throws SQLException {
        pstmt = conn.prepareStatement(sql);
        //将sql语句中的？替换为具体的值
        for (int i = 0;i<values.length;i++) {
            pstmt.setObject(i + 1,values[i]);
        }
        int isRight = pstmt.executeUpdate();
       return isRight;
    }

    /**
     * 释放资源
     */

    public static void main(String[] args) {

        DBUtil dbUtil = new DBUtil();
        try {
            //1. 连接数据库
            dbUtil.getConnection();
            int r = dbUtil.update("insert into admin(username,password) values(?,?)","abc","123456");
            if(r==1) {
                System.out.println("添加成功......");
            }else {
                System.out.println("失败......");
            }
            //2. 调用查询的方法
//            ResultSet rs = dbUtil.query("select * from admin");
//            while (rs.next()) {
//                int id = rs.getInt("id");
//                String username = rs.getString("username");
//                String password = rs.getString("password");
//                System.out.println(id + "--" + username + "--" + password);
//            }
//            ResultSet rs = dbUtil.query("select * from dept");
//                        List<Dept> list = new ArrayList<>();
//                        while (rs.next()) {
//                            int deptno = rs.getInt("deptno");
//                            String dname = rs.getString("dname");
//                            String loc = rs.getString("loc");
//                            //创建dept对象
//                            Dept dept = new Dept();
//                            dept.setDeptno(deptno);
//                            dept.setDname(dname);
//                            dept.setLoc(loc);
//                            list.add(dept);
//                            //System.out.println(deptno + "---" + dname + "---" + loc);
//                        }
//                        for (Dept d:list) {
//                            System.out.println(d.getDeptno() + "---" + d.getDname());
//                        }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
